package com.mmall.controller;

import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.param.UserParm;
import com.mmall.pojo.SysRole;
import com.mmall.pojo.SysUser;
import com.mmall.service.ISysRoleService;
import com.mmall.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author wangtengyu
 * @Create 2018-03-10-9:43
 */
@Controller
@RequestMapping("/sys/user")
public class sysUserController {
    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 保存用户
     * @param userParm
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public ServerResponse saveUser(UserParm userParm){
        iSysUserService.save(userParm);
        return ServerResponse.createBySuccess("新增用户成功");
    }

    /**
     * 更新用户
     * @param userParm
     * @return
     */
    @RequestMapping("update.json")
    @ResponseBody
    public ServerResponse updateUser(UserParm userParm){
        iSysUserService.update(userParm);
        return ServerResponse.createBySuccess("更新用户成功");
    }

    /**
     * 获取用户列表
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list.json")
    @ResponseBody
    public ServerResponse userList(int deptId,int pageNum,int pageSize){
        PageInfo<SysUser> pageInfo=iSysUserService.getUserList(deptId,pageNum,pageSize);
        return ServerResponse.createBySuccess(pageInfo);
    }


    @RequestMapping("delete.json")
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") int userId){
        iSysUserService.delete(userId);
        return ServerResponse.createBySuccess("删除用户成功");
    }


    @RequestMapping("userbindedRole.json")
    @ResponseBody
    public ServerResponse userBindedRole(@RequestParam("userId") int userId){
        List<SysRole> roleList=iSysRoleService.getRoleListByUserId(userId);
        return ServerResponse.createBySuccess(roleList);
    }



    @RequestMapping("noAuth.page")
    public ModelAndView noAuth(){
        return new ModelAndView("noAuth");
    }




}
